package com.example.arithmeticleetcode.leetcode.october;

import com.example.arithmeticleetcode.learnArithmetic.set.ListSet;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.TreeSet;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-10-21 10:37
 **/
public class Demo11 {

    public static void main(String[] args) {
        String date1 = "2019-01-09";
        String date2 = "2019-02-10";
        String date3 = "2003-03-01";
        String date4 = "2004-03-01";
        String date5 = "1900-03-25";
        System.out.println(dayOfYear(date1));
        System.out.println(dayOfYear(date2));
        System.out.println(dayOfYear(date3));
        System.out.println(dayOfYear(date4));
        System.out.println(dayOfYear(date5));

        System.out.println("-------------------------------");

        System.out.println(dayOfYear1(date1));
        System.out.println(dayOfYear1(date2));
        System.out.println(dayOfYear1(date3));
        System.out.println(dayOfYear1(date4));
        System.out.println(dayOfYear1(date5));

    }

    /*平年二月28天*/
    private static final int DAYS_28 = 28;
    /*闰年二月29天*/
    private static final int DAYS_29 = 29;
    /*除了31天的月份其他均为30天*/
    private static final int DAYS_30 = 30;
    /*1、3、5、7、8、10、12月份31天*/
    private static final int DAYS_31 = 31;


    public static int dayOfYear(String date) {
        String[] split = date.split("-");
        int totalDays = 0;
        int year = Integer.valueOf(split[0]);
        int month = Integer.valueOf(split[1]);
        int day = Integer.valueOf(split[2]);
        switch (month) {
            // 12 月份加的是11月份的天数，依次类推
            case 12:
                totalDays += DAYS_30;
            case 11:
                totalDays += DAYS_31;
            case 10:
                totalDays += DAYS_30;
            case 9:
                totalDays += DAYS_31;
            case 8:
                totalDays += DAYS_31;
            case 7:
                totalDays += DAYS_30;
            case 6:
                totalDays += DAYS_31;
            case 5:
                totalDays += DAYS_30;
            case 4:
                totalDays += DAYS_31;
            case 3:
                // 判断是否是闰年
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    totalDays += DAYS_29;
                } else {
                    totalDays += DAYS_28;
                }
            case 2:
                totalDays += DAYS_31;
            case 1: // 如果是1月份就加上输入的天数
                totalDays += day;
        }

        return totalDays;
    }

    public static int dayOfYear1(String date) {
        try {
            Calendar ca = Calendar.getInstance();
            ca.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
            return ca.get(Calendar.DAY_OF_YEAR);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
